package com.venutech.gongan.api;

import com.venutech.gongan.dao.*;
import com.venutech.gongan.dto.DeviceCountDTO;
import com.venutech.gongan.dto.CountByStatusDTO;
import com.venutech.gongan.dto.WorkCountByTypeAndStatusDTO;
import com.venutech.gongan.vo.DashboardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.stream.Collectors;

@Api(tags = "看板API")
@RestController
@RequestMapping("dashboard")
public class DashboardController {

    @Resource
    private QdsxjMapper qdsxjMapper;

    @Resource
    private QRCodeMapper qrCodeMapper;

    @Resource
    private JkgwMapper jkgwMapper;

    @Resource
    private WorkMapper workMapper;

    @Resource
    private OpWorkMapper opWorkMapper;

    @ApiOperation("看板概览")
    @GetMapping("overview")
    public DashboardVO dashboard() {
//        Map<String, Integer> deviceOverview = new HashMap<>();
//        qdsxjMapper.countByDeviceType().forEach(deviceOverview::putAll);

        return DashboardVO.builder()
                .deviceCount(qdsxjMapper.countAll())
                .deviceOverview(qdsxjMapper.countByDeviceType().stream().collect(Collectors.toMap(DeviceCountDTO::getType, DeviceCountDTO::getCount)))
                .jkgwQrCodeCount(qrCodeMapper.countAll())
                .jkgwCount(jkgwMapper.countAll())
                .jkgwBindDeviceCount(qdsxjMapper.countBindGw())
                .opWorkCount(opWorkMapper.countByStatus()
                        .stream()
                        .collect(
                                Collectors.toMap(
                                        CountByStatusDTO::getStatus,
                                        CountByStatusDTO::getCount
                                )
                        ))
                .workCount(workMapper.countByStatus()
                        .stream()
                        .collect(Collectors.toMap(
                                CountByStatusDTO::getStatus,
                                CountByStatusDTO::getCount
                        )))
                .workCountMap(workMapper.countByTypeAndStatus().stream()
                        .collect(Collectors.groupingBy(
                                WorkCountByTypeAndStatusDTO::getType,
                                Collectors.toMap(
                                        WorkCountByTypeAndStatusDTO::getStatus,
                                        WorkCountByTypeAndStatusDTO::getCount
                                )
                        )))
                .opWorkCountMap(opWorkMapper.countByTypeAndStatus()
                        .stream()
                        .collect(Collectors.groupingBy(
                                WorkCountByTypeAndStatusDTO::getType,
                                Collectors.toMap(
                                        WorkCountByTypeAndStatusDTO::getStatus,
                                        WorkCountByTypeAndStatusDTO::getCount
                                )
                        )))
                .build();
    }
}
